﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Diagnostics;

namespace whereIsTheDonutGame
{
    public class Skybox : Microsoft.Xna.Framework.DrawableGameComponent
    {
        ContentManager content;
        ICamera camera;

        Model mesh;
        TextureCube texture;
        Effect effect;
        public int size = 256;

        public Skybox(donutGame game)
            : base(game)
        {
            content = new ContentManager(Game.Services, "Content");
        }

        public int getSkyboxSize()
        {
            return size;
        }

        public override void Initialize()
        {
            camera = (ICamera)Game.Services.GetService(typeof(ICamera));
            base.Initialize();
        }

        protected override void LoadContent()
        {
            mesh = content.Load<Model>("cube");
            texture = content.Load<TextureCube>("Skybox/SkyBoxTex");
            effect = content.Load<Effect>("Skybox/Skybox");
            base.LoadContent();
        }

        public void drawSkybox()
        {
            SamplerState ss = new SamplerState();
            ss.AddressU = TextureAddressMode.Clamp;
            ss.AddressV = TextureAddressMode.Clamp;
            GraphicsDevice.SamplerStates[0] = ss;

            DepthStencilState dss = new DepthStencilState();
            dss.DepthBufferEnable = false;
            GraphicsDevice.DepthStencilState = dss;


            LoadContent();

            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                foreach (ModelMesh meshes in mesh.Meshes)
                {
                    foreach (ModelMeshPart part in meshes.MeshParts)
                    {
                        part.Effect = effect;
                        part.Effect.Parameters["World"].SetValue(Matrix.CreateScale(size) * Matrix.CreateScale(2.0f));
                        part.Effect.Parameters["View"].SetValue(camera.ViewMatrix);
                        part.Effect.Parameters["Projection"].SetValue(camera.ProjectionMatrix);
                        part.Effect.Parameters["SkyBoxTexture"].SetValue(texture);
                    }
                    meshes.Draw();
                }
            }

            dss = new DepthStencilState();
            dss.DepthBufferEnable = true;
            GraphicsDevice.DepthStencilState = dss;
        }
    }
}
